"""地址管理 Schema"""
from datetime import datetime
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, Field

FIELD = Field(..., max_length=128, description="地址名称")


class AddressBase(BaseModel):
	"""地址基础字段"""
	name: str = FIELD
	full_name: str = Field(..., max_length=512, description="完整地址")
	level: int = Field(..., ge=1, le=7, description="地址级别: 1-国家, 2-省, 3-市, 4-区县, 5-街道, 6-社区, 7-详细地址")
	region_code: Optional[str] = Field(None, max_length=12, description="区-行政区划码")
	city_code: Optional[str] = Field(None, max_length=12, description="市-行政区划码")
	province_code: Optional[str] = Field(None, max_length=12, description="省-行政区划码")
	city_name: Optional[str] = Field(None, max_length=12, description="市-行政区划码")
	province_name: Optional[str] = Field(None, max_length=12, description="省-行政区划码")
	longitude: Decimal = Field(..., description="经度")
	latitude: Decimal = Field(..., description="纬度")
	source: str = Field("CUSTOM", max_length=16, description="数据来源")
	is_active: bool = Field(True, description="是否启用")


class AddressCreate(AddressBase):
	"""创建地址请求"""
	parent_id: Optional[int] = Field(None, description="父级地址ID")


class AddressUpdate(BaseModel):
	"""更新地址请求"""
	name: Optional[str] = Field(None, max_length=128, description="地址名称")
	full_name: Optional[str] = Field(None, max_length=512, description="完整地址")
	longitude: Optional[Decimal] = Field(None, description="经度")
	latitude: Optional[Decimal] = Field(None, description="纬度")
	is_active: Optional[bool] = Field(None, description="是否启用")


class AddressResponse(AddressBase):
	"""地址响应"""
	id: int = Field(..., description="主键ID")
	parent_id: Optional[int] = Field(None, description="父级地址ID")
	created_at: datetime = Field(..., description="创建时间")
	updated_at: datetime = Field(..., description="更新时间")
	# 新增省市名称字段
	province_name: Optional[str] = Field(None, description="省份名称")
	city_name: Optional[str] = Field(None, description="城市名称")


class DeviceAddressCreate(BaseModel):
	"""创建设备地址关联请求"""
	device_type: str = Field(..., max_length=24, description="设备类型(STATION,BOX,RTU,METER)")
	device_id: int = Field(..., description="设备ID")
	device_code: Optional[str] = Field(None, description="设备code")
	address_id: int = Field(..., description="地址ID")
	address_type: str = Field("INSTALLATION", max_length=24, description="地址类型")
	is_primary: bool = Field(True, description="是否主地址")
	description: Optional[str] = Field(None, max_length=255, description="地址描述")


class DeviceAddressUpdate(BaseModel):
	"""更新设备地址关联请求"""
	address_id: Optional[int] = Field(None, description="地址ID")
	address_type: Optional[str] = Field(None, max_length=24, description="地址类型")
	is_primary: Optional[bool] = Field(None, description="是否主地址")
	description: Optional[str] = Field(None, max_length=255, description="地址描述")


class DeviceAddressResponse(BaseModel):
	"""设备地址关联响应"""
	id: int = Field(..., description="主键ID")
	device_type: str = Field(..., description="设备类型")
	device_id: int = Field(..., description="设备ID")
	address_id: int = Field(..., description="地址ID")
	address_type: str = Field(..., description="地址类型")
	is_primary: bool = Field(..., description="是否主地址")
	description: Optional[str] = Field(None, description="地址描述")
	created_at: datetime = Field(..., description="创建时间")

	# 关联的地址信息
	address_name: Optional[str] = Field(None, description="地址名称")
	full_address: Optional[str] = Field(None, description="完整地址")
	longitude: Optional[Decimal] = Field(None, description="经度")
	latitude: Optional[Decimal] = Field(None, description="纬度")


class DeviceLocationInfo(BaseModel):
	"""设备位置信息"""
	address_id: Optional[int] = Field(None, description="地址ID")
	address_name: Optional[str] = Field(None, description="地址名称")
	full_address: Optional[str] = Field(None, description="完整地址")
	longitude: Optional[Decimal] = Field(None, description="经度")
	latitude: Optional[Decimal] = Field(None, description="纬度")
	address_type: Optional[str] = Field(None, description="地址类型")
	is_primary: Optional[bool] = Field(None, description="是否主地址")


class AddressListRequest(BaseModel):
	"""地址列表查询请求"""
	keyword: Optional[str] = Field(None, description="关键词搜索")
	level: Optional[int] = Field(None, ge=1, le=7, description="地址级别")
	parent_id: Optional[int] = Field(None, description="父级地址ID")
	is_active: Optional[bool] = Field(None, description="是否启用")


class AddressListResponse(BaseModel):
	"""地址列表响应"""
	total: int = Field(..., description="总数")
	items: list[AddressResponse] = Field(..., description="地址列表")


class DeviceAddressListRequest(BaseModel):
	"""设备地址关联列表查询请求"""
	device_type: Optional[str] = Field(None, description="设备类型")
	device_id: Optional[int] = Field(None, description="设备ID")
	address_type: Optional[str] = Field(None, description="地址类型")
	is_primary: Optional[bool] = Field(None, description="是否主要地址")


class DeviceAddressListResponse(BaseModel):
	"""设备地址关联列表响应"""
	total: int = Field(..., description="总数")
	items: list[DeviceAddressResponse] = Field(..., description="设备地址关联列表")
